#!/bin/bash
#Author: weike
#MyQQID: 2362317758
export TOPDIR=$PWD

export MODPKG_ROOT=${TOPDIR}/pkg
export MODSRC_ROOT=${TOPDIR}/src

export MODNAME=rc.local

export MODPKG_NAME=${MODNAME}.tar.gz
export MODSRC_NAME=${MODNAME}

export MODPKG_PATH=${MODPKG_ROOT}/${MODPKG_NAME}
export MODSRC_PATH=${MODSRC_ROOT}/${MODSRC_NAME}
export MODOUT_PATH=$TOPDIR/output

export PORT_CFGLIST_NAME=port_conf.lst
export PORT_CFGHEAD_NAME=port_conf.h
export PORT_CFGROOT_PATH=${MODSRC_PATH}/config

export PORT_CFGLIST_PATH=${PORT_CFGROOT_PATH}/${PORT_CFGLIST_NAME}
export PORT_CFGHEAD_PATH=${MODSRC_PATH}/${PORT_CFGHEAD_NAME}

#-----------------------------------------------------------------------
perro()
{
    echo -e "\033[47;41mERROR: $*\033[0m"
}

pwarn()
{
    echo -e "\033[47;43mWARN: $*\033[0m"
}

pinfo()
{
    echo -e "\033[47;44mINFO: $*\033[0m"
}

pdone()
{
    echo -e "\033[47;42mINFO: $*\033[0m"
}

#-----------------------------------------------------------------------

append_head()
{
	echo -e "#ifndef _PORT_CONF_H"    >  ${PORT_CFGHEAD_PATH}
	echo -e "#define _PORT_CONF_H "   >> ${PORT_CFGHEAD_PATH}
	echo -e "#include <ual_types.h>" >> ${PORT_CFGHEAD_PATH}
	echo -e "ual_port_node ual_port_node_list[] = {" >> ${PORT_CFGHEAD_PATH}

}

append_tail()
{
	echo  "};"     >> ${PORT_CFGHEAD_PATH}
	echo  "#endif" >> ${PORT_CFGHEAD_PATH}
}

apend_node_dout()
{
	echo  "    {" >> ${PORT_CFGHEAD_PATH}
	echo  "        .type = dout,"       >> ${PORT_CFGHEAD_PATH}
	echo  "        .name = \"${1}\"," >> ${PORT_CFGHEAD_PATH}
	echo  "        .port = \"${2}\","     >> ${PORT_CFGHEAD_PATH}
	echo  "        .defv = \"${3}\","     >> ${PORT_CFGHEAD_PATH}
	echo  "    }," >> ${PORT_CFGHEAD_PATH}
}

apend_node_din()
{
	echo  "    {" >> ${PORT_CFGHEAD_PATH}
	echo  "        .type = din,"       >> ${PORT_CFGHEAD_PATH}
	echo  "        .name = \"${1}\"," >> ${PORT_CFGHEAD_PATH}
	echo  "        .port = \"${2}\","     >> ${PORT_CFGHEAD_PATH}
	echo  "        .defv = \"${3}\","     >> ${PORT_CFGHEAD_PATH}
	echo  "    }," >> ${PORT_CFGHEAD_PATH}
}

apend_node_dlink()
{
	echo  "    {" >> ${PORT_CFGHEAD_PATH}
	echo  "        .type = dlink,"       >> ${PORT_CFGHEAD_PATH}
	echo  "        .name = \"${1}\"," >> ${PORT_CFGHEAD_PATH}
	echo  "        .path = \"${2}\","     >> ${PORT_CFGHEAD_PATH}
	echo  "    }," >> ${PORT_CFGHEAD_PATH}
}

set_ual_dout()
{
	tmpline=$1
	is_do_type=$(echo $tmpline | grep -w "type=dout")
	if [ -n "${is_do_type}" ]; then
		arryline=$(echo $tmpline | sed 's/,/ /g')
		for kvgrp in $arryline
		do
			key=${kvgrp%%=*}
			val=${kvgrp##*=}
			if [ -n "${key}" ] && [ -n "${val}" ]; then
				if [ "xname" = "x${key}" ]; then
					export do_name=${val}
				elif [ "xport" = "x${key}" ]; then
					export do_port=${val}
				elif [ "xdefv" = "x${key}" ]; then
					export do_defv=${val}
				fi
			fi
		done
		if [ -n "${do_name}" ] && [ -n "${do_port}" ] && [ -n "${do_defv}" ]; then
			apend_node_dout ${do_name} ${do_port} ${do_defv}
		fi
		unset do_name
		unset do_port
		unset do_defv
	fi
}

set_ual_din()
{
	tmpline=$1
	is_do_type=$(echo $tmpline | grep -w "type=din")
	if [ -n "${is_do_type}" ]; then
		arryline=$(echo $tmpline | sed 's/,/ /g')
		for kvgrp in $arryline
		do
			key=${kvgrp%%=*}
			val=${kvgrp##*=}
			if [ -n "${key}" ] && [ -n "${val}" ]; then
				if [ "xname" = "x${key}" ]; then
					export do_name=${val}
				elif [ "xport" = "x${key}" ]; then
					export do_port=${val}
				elif [ "xdefv" = "x${key}" ]; then
					export do_defv=${val}
				fi
			fi
		done
		if [ -n "${do_name}" ] && [ -n "${do_port}" ] && [ -n "${do_defv}" ]; then
			apend_node_din ${do_name} ${do_port} ${do_defv}
		fi
		unset do_name
		unset do_port
		unset do_defv
	fi
}

set_ual_dlink()
{
	tmpline=$1
	is_link_type=$(echo $tmpline | grep -w "type=dlink")
	if [ -n "${is_link_type}" ]; then
		arryline=$(echo $tmpline | sed 's/,/ /g')
		for kvgrp in $arryline
		do
			key=${kvgrp%%=*}
			val=${kvgrp##*=}
			if [ -n "${key}" ] && [ -n "${val}" ]; then
				if [ "xname" = "x${key}" ]; then
					export link_name=${val}
				elif [ "xpath" = "x${key}" ]; then
					export link_path=${val}
				fi
			fi
		done
		if [ -n "${link_name}" ] && [ -n "${link_path}" ]; then
			apend_node_dlink ${link_name} ${link_path}
		fi
		unset link_name
		unset link_path
	fi
}

parse_port_conf()
{
	while read line
	do
		procline=$(echo $line | sed 's/ //g')
		validline=$(echo $procline | grep -v "^#")
		validline=$(echo $validline | grep  "^type=")
		if [ -z "$validline" ]; then
			continue;
		fi
		set_ual_dout   $procline
		set_ual_din    $procline
		set_ual_dlink  $procline
	done < ${PORT_CFGLIST_PATH}
}


if [ ! -e ${PORT_CFGLIST_PATH} ]; then
	perro "NO PORT_CFGLIST_PATH: ${PORT_CFGLIST_PATH}"
	exit 1
fi
pinfo "Start create: $PORT_CFGHEAD_PATH"
append_head
parse_port_conf
append_tail
pdone "Finish create: $PORT_CFGHEAD_PATH"


